package com.kg.rails.component.editor.util;

import com.kg.rails.ui.Application;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Resources {

    public static final String RESOURCE_LANGUAGE_ARGUMENT = "-language=";

    /**
     * Ordered list of the inserted resource bundles.
     */
    protected static List<ResourceBundle> bundles = new LinkedList<ResourceBundle>();

    static {
        try {
            ((LinkedList<ResourceBundle>) bundles).addFirst(new PropertyResourceBundle(new InputStreamReader(Application.class.getResourceAsStream("resources/editor.properties"))));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    public static void setLanguage(String language) {
        bundles.clear();

        try {
            ((LinkedList<ResourceBundle>) bundles).addFirst(new PropertyResourceBundle(new InputStreamReader(Application.class.getResourceAsStream("resources/editor.properties"))));

            if (language == null || "ru".equals(language)) {
                ((LinkedList<ResourceBundle>) bundles).addFirst(new PropertyResourceBundle(new InputStreamReader(Application.class.getResourceAsStream("resources/editor_ru.properties"))));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static String get(String key) {
        return get(key, null, null);
    }

    public static String get(String key, String defaultValue) {
        return get(key, null, defaultValue);
    }

    /**
     * Returns the value for the specified resource key.
     */
    public static String get(String key, String[] params) {
        return get(key, params, null);
    }

    /**
     * Returns the value for the specified resource key.
     */
    public static String get(String key, String[] params, String defaultValue) {
        String value = getResource(key);

        // Applies default value if required
        if (value == null) {
            value = defaultValue;
        }

        // Replaces the placeholders with the values in the array
        if (value != null && params != null) {
            String index = null;
            StringBuilder result = new StringBuilder();

            for (int i = 0; i < value.length(); i++) {
                char c = value.charAt(i);

                if (c == '{') {
                    index = "";
                } else if (index != null && c == '}') {
                    int tmp = Integer.parseInt(index) - 1;

                    if (tmp >= 0 && tmp < params.length) {
                        result.append(params[tmp]);
                    }

                    index = null;
                } else if (index != null) {
                    index += c;
                } else {
                    result.append(c);
                }
            }

            value = result.toString();
        }

        return value;
    }

    /**
     * Returns the value for <code>key</code> by searching the resource
     * bundles in inverse order or <code>null</code> if no value can be found
     * for <code>key</code>.
     */
    protected static String getResource(String key) {
        for (ResourceBundle bundle : bundles) {
            try {
                return bundle.getString(key);
            } catch (MissingResourceException mrex) {
                // continue
            }
        }

        return null;
    }
}
